package com.smartEdu.module.schedule;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.smartEdu.module.schedule.GobalConstant;
import com.smartEdu.utils.IOUtils;

	/*
	 *查询个人成绩方法
	 *@throws ClientProtocolException
	 *@throws IOException 
	 * */
public class MarkInquiry {
	private String queryStumarkUrl = GobalConstant.QUERY_STU_MARK_URL;
	private String queryStulevlmarkUrl = GobalConstant.QUERY_STU_levlMARK_URL;
	
	public void queryStuMark() 
			throws ClientProtocolException, IOException{
		CloseableHttpClient client = HttpClients.createDefault();
		HttpPost queryMarkPost = new HttpPost(queryStumarkUrl);
		queryMarkPost.setHeader("Cookie",Login.Cookie);
		//封装请求参数
		List<NameValuePair> queryMarkPair = new ArrayList<NameValuePair>();
		UrlEncodedFormEntity entityMark = new UrlEncodedFormEntity(
				queryMarkPair);
		queryMarkPost.setEntity(entityMark);
		HttpResponse responQueryMarkPost = client.execute(queryMarkPost);
		String markHtml = IOUtils.getHtml(responQueryMarkPost.getEntity()
				.getContent(), "utf-8");
		Document markDoc = Jsoup.parse(markHtml);
		Elements eleMark = markDoc.select("table").select("tr").select("td");
//		测试		
//		Elements testElement = markDoc.select("table").select("tr").select("td");
//		for(int i = 0; i < testElement.size() ; ++i){
//			System.out.println(i+" "+testElement.get(i).text());
//		}		
		JSONObject json = new JSONObject();
		JSONArray jsonMark = new JSONArray();
		for(int i = 1; i < eleMark.size() ; i += 10){
			JSONObject jsonElem = new JSONObject();
			try {
				jsonElem.put("SID", eleMark.get(i).text());//SID序号
				jsonElem.put("Semester", eleMark.get(i+1).text());//Semester开课学期
				jsonElem.put("CourseID", eleMark.get(i+2).text());//CourseName课程名称
				jsonElem.put("CourseName", eleMark.get(i+3).text());//CourseID课程编号
				jsonElem.put("CourseGrade", eleMark.get(i+4).text());//CourseGrade课程成绩
				jsonElem.put("CourseCredit", eleMark.get(i+5).text());//CourseCredit学分
				jsonElem.put("CourseProperties", eleMark.get(i+8).text());//CourseProperties课程属性
				jsonElem.put("CourseNature", eleMark.get(i+9).text());//CourseNature课程性质
				jsonMark.put(jsonElem);
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
		try {
			json.put("Mark", jsonMark);
		} catch (Exception e) {
			// TODO: handle exception
		}

		System.out.println(json.toString());		
		client.close();
	}

	/*
	 *查询等级考试成绩方法
	 *@throws ClientProtocolException
	 *@throws IOException 
	 * */

	public void queryLevlMark() 
			throws ClientProtocolException, IOException{
	CloseableHttpClient client = HttpClients.createDefault();
	HttpPost queryLevlMarkPost = new HttpPost(queryStulevlmarkUrl);
	queryLevlMarkPost.setHeader("Cookie",Login.Cookie);
	//封装请求参数
	List<NameValuePair> queryLevlMarkPair = new ArrayList<NameValuePair>();
	UrlEncodedFormEntity entityMark = new UrlEncodedFormEntity(
			queryLevlMarkPair);
	queryLevlMarkPost.setEntity(entityMark);
	HttpResponse responQueryLevlMarkPost = client.execute(queryLevlMarkPost);
	String markHtml = IOUtils.getHtml(responQueryLevlMarkPost.getEntity()
			.getContent(), "utf-8");
	Document markDoc = Jsoup.parse(markHtml);
	Elements eleLevlMark = markDoc.getElementById("dataList").select("table").select("tr").select("td");//按需求解析html<td>标签内容并输出
	JSONObject json = new JSONObject();
	JSONArray jsonLevlMark = new JSONArray();
//	测试
//	for(int j = 0 ;  j < eleLevlMark.size() ; j++)
//	{
//		if((j+1)/2 % 10 != 0){
//			System.out.println("**************");
//		}
//		System.out.println(j+" " + eleLevlMark.get(j).text());
		
//	}
	for(int i = 0 ; i < eleLevlMark.size() ; i += 9){
		JSONObject jsonElem = new JSONObject();
		try {
			jsonElem.put("SID", eleLevlMark.get(i).text());	//SID序号
			jsonElem.put("ExamName", eleLevlMark.get(i+1).text());//ExamName等级考试名称
			jsonElem.put("F_PaperGrade", eleLevlMark.get(i+2).text());//F_PaperGrade分数类笔试成绩
			jsonElem.put("F_ComputerGrade", eleLevlMark.get(i+3).text());//F_ComputerGrade分数类上机成绩
			jsonElem.put("F_TotalMark",eleLevlMark.get(i+4).text());//F_TotalMark分数类总成绩
			
			jsonElem.put("L_PaperGrade", eleLevlMark.get(i+6).text());//L_PaperGrade等级类笔试成绩
			jsonElem.put("L_ComputerGrade", eleLevlMark.get(i+7).text());//L_ComputerGrade等级类机试成绩
			jsonElem.put("L_TotalMark",eleLevlMark.get(i+7).text());//L_totalMark等级类总成绩
			
			jsonElem.put("ExamDate", eleLevlMark.get(i+8).text());//ExamDate考试日期
	
			jsonLevlMark.put(jsonElem);		
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
		try {
			json.put("LevlMark", jsonLevlMark);
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(json.toString());	
	client.close();
	}
}
